*******************************************************************************
* Load data
*******************************************************************************

use dataset_polls, clear

*******************************************************************************
* Keep only polls where trust asked
*******************************************************************************

drop if mi(trustc) & mi(trustflc)

*******************************************************************************
* If both vote choice and party included, take the average 
*******************************************************************************

replace subsample = "Dem" if regex(lower(subsample), "dem") 
replace subsample = "Rep" if regex(lower(subsample), "rep")
keep if inlist(subsample, "Dem", "Rep")

collapse legitimatec trustc trustflc cycle, ///
	by(subsample date house2)

*******************************************************************************
* Reshape to date-house-subsample level
*******************************************************************************

gen datehouse = string(date, "%d") + "-" + house2
reshape i datehouse
reshape j subsample, string
reshape xi date house2 cycle
reshape xij legitimatec trustc trustflc 
reshape wide		

*******************************************************************************
* Plot
*******************************************************************************

foreach subsample in Dem Rep {

format date %d

#delimit;

gr tw	
	(sc legitimatec`subsample' date, msym(s) col(black))
	(sc trustc`subsample' date, msym(Oh) col(black))
	(sc trustflc`subsample' date, msym(th) col(black))
	,
		ylab(, angle(horiz) labcol(black))
		xlab(, labcol(black))
		legend(off)
		plotregion(style(none))
		ytitle("")
		xtitle("")
		by(cycle, xrescale legend(off) note("") cols(1) 
			title(`subsample', col(black)))
		subtitle(, fcol(none) lwid(none) col(black))
		name(g`subsample', replace)
		xscale(lcol(black))
		yscale(lcol(black))		
		;
		
#delimit cr

}

*******************************************************************************
* Combine plots
*******************************************************************************

gr combine gDem gRep, xsize(3) ysize(2)

*******************************************************************************
* Save
*******************************************************************************

gr export "_output/figure_appendix_trust.pdf", replace

*******************************************************************************
* End
*******************************************************************************
